package com.example.lxy.mapper;

import com.example.lxy.pojo.access;
import com.example.lxy.pojo.user;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    //注册
    @Insert("INSERT INTO USER(username,PASSWORD,nickname,sex,email,phone,deptid,jueseid) VALUES(#{username},#{password},#{nickname},#{sex},#{email},#{phone},#{deptid},#{jueseid})")
    boolean add(user u);

    //注册时查询用户名
    @Select("select * from user where username=#{username}")
    user reglist(String username);

    //注册时根据昵称查询
    @Select("select * from user where nickname=#{nickname}")
    user reglist1(String nickname);

    //登录
    @Select("select * from user where username=#{username} and password=#{password}")
    user login(@Param("username")String username,@Param("password")String password);

    //根据用户查权限菜单
    @Select("SELECT * FROM role, access,roleaccess WHERE role.`rid` = roleaccess.`roleid` AND access.`id` = roleaccess.`accessid` AND role.`rid` = (SELECT role.`rid` FROM USER,role,userrole WHERE user.`jueseid` = userrole.`userid` AND role.`rid` = userrole.`roleid` AND user.nickname = #{nickname})")
    List<user> list(String nickname);

    //根据用户查权限菜单
    List<access> lists();

    //多表查询用户
    @Select("SELECT u.uid,u.username,u.password,u.nickname,u.sex,u.email,u.phone,u.status,d.`name`,rs.rolename FROM USER u,dept d,role rs WHERE u.deptid=d.id AND u.jueseid=rs.rid")
    List<user> listbiao();

    //删除用户
    @Delete("delete from user where uid=#{uid}")
    void deluser(int uid);

    //根据用户ID查
    @Select("select * from user where uid=#{uid}")
    user upiduser(int uid);

    //修改用户
    @Update("update user set username=#{username},password=#{password},nickname=#{nickname},sex=#{sex},email=#{email},phone=#{phone},deptid=#{deptid},status=#{status},jueseid=#{jueseid} where uid=#{uid}")
    void updateuser(user u);

    //模糊查询
    @Select("SELECT u.uid,u.username,u.password,u.nickname,u.sex,u.email,u.phone,u.status,d.`name`,rs.rolename FROM USER u,dept d,role rs WHERE u.nickname like #{nickname} and u.deptid=d.id AND u.jueseid=rs.rid")
    List<user> likelist(String nickname);
}
